type TimerFun = (currTime: number) => boolean;

class Timer {
    private timerId = 0;
    private timers: TimerFun[] = [];
    private currTime: number;
    add(fn: TimerFun) {
        this.timers.push(fn)
    }

    start() {
        if (this.timerId) return;
        this.runNext()
    }
    runNext() {
        if (this.timers) {
            this.currTime = +new Date();
            for (var i = 0; i < this.timers.length; i++) {
                if (!this.timers[i](this.currTime)) {
                    this.timers.splice(i, 1);
                    i--;
                }
            }
        }
        requestAnimationFrame(this.runNext.bind(this))
        //this.timerId = setTimeout(this.runNext.bind(this), 0);
    }

    stop() {
        clearTimeout(this.timerId)
        this.timerId = 0;
    }
}
 